// pages/user/register/verify/verify.js
var aliyun_api = require('../../../utils/aliyun-api-gateway.js');
var util = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    vercode: '123'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  formsubmit1: function (e) {
    console.log(e.detail.value.phone)
    console.log("获取验证码")
    
    var that = this
    const params = {
      url: 'https://api.link.aliyun.com/kvstore/get',
      apiVer: '1.1.0',
      param: {
        "key": e.detail.value.phone
      }
    }
    aliyun_api.request(params).then(function (res) {
      //  console.log(data)
      if (res.data) {
        var vercode = util.SendSms(e.detail.value.phone)
        that.setData({
          vercode: vercode
        })
      } else {
        wx.showToast({
          title: '账号不存在，请前往app注册',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  formsubmit: function (e) {
    console.log(e.detail.value)
    console.log("下一步")

    var that = this
    if (e.detail.value.phone === '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (e.detail.value.vercode === '') {
      wx.showToast({
        title: '请填写验证码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (e.detail.value.vercode === that.data.vercode) {
      try { //存入缓存
        wx.setStorageSync('accountInfo', e.detail.value.phone)
        //wx.setStorageSync('password', e.detail.value.password)
      } catch (e) {
        console.log('fail')
      }

      wx.switchTab({
        url: '../../home/home',
      })
    }
    else{
      wx.showToast({
        title: '验证码错误',
        icon: 'none',
        duration: 2000
      })
    }
  },
})